import getApiData from '@/services/cpasRequest';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useModel } from 'umi';
export default function Page() {
  const globalModel = useModel('@@initialState') as any;

  const dbname = globalModel?.initialState?.curPart?.dbname;

  const [dataList, setDataList] = useState([]);

  const refresh = useCallback(() => {
    if (dbname) {
      getApiData('home.zcbfAuditSection', { zcbfid: dbname })
        .then((responseData) => {
          setDataList(responseData.data);
        })
        .catch((error) => {
          console.error(error);
        });
    }
  }, [dbname]);

  useEffect(() => {
    refresh();
  }, [dbname, refresh]);

  const qjYearData = useMemo(() => {
    if (dataList && dataList.length > 0) {
      const newDataList = dataList.map((item: any) => {
        return item.Data_ID;
      });
      const sortData = newDataList.sort((a, b) => {
        return a - b;
      });
      return sortData;
    }
    return [];
  }, [dataList]);

  const getDefaultValue = useCallback(() => {
    return qjYearData;
  }, [qjYearData]);

  return {
    dataList,
    qjYearData,
    getDefaultValue,
    refresh,
  };
}
